﻿// <copyright project="Salient.SqlServer.NHibernate" file="IdiotPKCompositeUserType.cs" company="Sky Sanders">
// This source is a Public Domain Dedication.
// Please see http://spikes.codeplex.com/ for details.   
// Attribution is appreciated
// </copyright> 
// <version>1.0</version>
using System;

namespace Northwind.Tests.Salient
{
    /// <summary>
    /// Learning how to create a composite user type for keys that cannot be mapped native
    /// </summary>
    [Serializable]
    public class IdiotPk : IComparable<IdiotPk>, IEquatable<IdiotPk>, ICloneable
    {
        private int _salesOrderDetailId;
        private int _salesOrderId;

        public IdiotPk()
        {
        }

        public IdiotPk(int salesOrderId)
        {
            _salesOrderId = salesOrderId;
        }

        public int SalesOrderId
        {
            get { return _salesOrderId; }
            set { _salesOrderId = value; }
        }

        public int SalesOrderDetailId
        {
            get { return _salesOrderDetailId; }
            set { _salesOrderDetailId = value; }
        }

        #region ICloneable Members

        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        /// A new object that is a copy of this instance.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public object Clone()
        {
            var other = new IdiotPk();
            other._salesOrderDetailId = _salesOrderDetailId;
            other._salesOrderId = _salesOrderId;
            return other;
        }

        #endregion

        #region IComparable<IdiotPk> Members

        /// <summary>
        /// Compares the current object with another object of the same type.
        /// </summary>
        /// <returns>
        /// A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has the following meanings: 
        ///                     Value 
        ///                     Meaning 
        ///                     Less than zero 
        ///                     This object is less than the <paramref name="other"/> parameter.
        ///                     Zero 
        ///                     This object is equal to <paramref name="other"/>. 
        ///                     Greater than zero 
        ///                     This object is greater than <paramref name="other"/>. 
        /// </returns>
        /// <param name="other">An object to compare with this object.
        ///                 </param>
        public int CompareTo(IdiotPk other)
        {
            if (SalesOrderDetailId == other.SalesOrderDetailId)
            {
                return 0;
            }
            return SalesOrderDetailId < other.SalesOrderDetailId ? -1 : 1;
        }

        #endregion

        #region IEquatable<IdiotPk> Members

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.
        ///                 </param>
        public bool Equals(IdiotPk other)
        {
            return other.SalesOrderDetailId == SalesOrderDetailId && other.SalesOrderId == SalesOrderId;
        }

        #endregion

        public override int GetHashCode()
        {
            return _salesOrderId.GetHashCode() + 1928 + _salesOrderDetailId.GetHashCode();
        }
    }
}